package net.starwifi.duobao.entity;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.Digits;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import java.math.BigDecimal;
import java.util.Date;

/**
 * Entity - 会员
 *
 * @author ruoo
 * @version 1.0
 */
@Entity
@Table(name = "duob_member")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "duob_member_sequence")
public class Member extends BaseEntity {

    private static final long serialVersionUID = 1533130686714725835L;

    /** "身份信息"参数名称 */
    public static final String PRINCIPAL_ATTRIBUTE_NAME =  "Member.PRINCIPAL";

    /** "用户名"Cookie名称 */
    public static final String COOKIE_USERNAME_NAME = "hu_username";

    /** "用户名"Cookie名称 */
    public static final String COOKIE_PASS_NAME = "hu_sp";

    /** 会员注册项值属性个数 */
    public static final int ATTRIBUTE_VALUE_PROPERTY_COUNT = 10;

    /** 会员注册项值属性名称前缀 */
    public static final String ATTRIBUTE_VALUE_PROPERTY_NAME_PREFIX = "attributeValue";

    /** 最大收藏商品数 */
    public static final Integer MAX_FAVORITE_COUNT = 10;

    /** 用户名 */
    private String username;

    /** salt 密码盐 */
    private String salt;

    /** 密码 */
    private String password;

    /* LOGO*/
    private String logo;

    /** E-mail */
    private String email;

    /** 是否启用 */
    private Boolean isEnabled;

    /** 是否锁定 */
    private Boolean isLocked;

    /** 连续登录失败次数 */
    private Integer loginFailureCount;

    /** 锁定日期 */
    private Date lockedDate;

    /** 注册IP */
    private String registerIp;

    /** 最后登录IP */
    private String loginIp;

    /** 最后登录日期 */
    private Date loginDate;

    /** 姓名 */
    private String name;

    /** 性别 */
    private int gender;

    /** 出生日期 */
    private Date birth;

    /** 地址 */
    private String address;

    /** 邮编 */
    private String zipCode;

    /** 电话 */
    private String phone;

    /** 手机 */
    private String mobile;

    /** 身份证号/营业执照 */
    private String cardNo;

    /** 生日 */
    private Date birthday;

    /** 昵称 */
    private String nickname;

    /** qq号码 */
    private String qq;

    /** 人民币余额 */
    private BigDecimal balance;

    /** */
    private BigDecimal currencyBalance;

    /** 虚拟币余额*/
    private BigDecimal virtualCurrency;

    /** 购物车 */
   // private Cart cart;

    /** 用户类型（0普通注册用户，1商家用户） */
    private int memberType;

    /*店铺名称*/
    private String shopName;

    /** 店铺标题*/
    private String shopTitle;

    /** 首页logo */
    private String shopBanner; /* 604*289 */

    /** wap站点 */
    private String url;

    /* 描述*/
    private String description;

    /** 是否上架 */
    private Boolean isMarketable;

    /** 是否置顶*/
    private Boolean isTop;

    private String bLicense; //营业执照

    private String bLicenseNo; //营业执照编号

    private String bBankName; //银行

    private String bBankCardNo; //银行卡编号

    private String bName; //法人名称

    private String bCardNo; //法人身份证编号

    private String cpId; //cp渠道商标识

    private String wxOpenId; //微信openID

    @JsonProperty
    @NotEmpty(groups = Save.class)
    @Pattern(regexp = "^[0-9a-z_A-Z\\u4e00-\\u9fa5]+$")
    @Column(nullable = false, updatable = false, unique = true, length = 100)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column
    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    @Column
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @JsonProperty
    @Pattern(regexp = "^[^\\s&\"<>]+$")
    @Column
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }



    @NotNull
    @Column(nullable = false)
    public Boolean getIsEnabled() {
        return isEnabled;
    }

    public void setIsEnabled(Boolean isEnabled) {
        this.isEnabled = isEnabled;
    }

    @Column(nullable = false)
    public Boolean getIsLocked() {
        return isLocked;
    }

    public void setIsLocked(Boolean isLocked) {
        this.isLocked = isLocked;
    }

    @Column
    public Integer getLoginFailureCount() {
        return loginFailureCount;
    }

    public void setLoginFailureCount(Integer loginFailureCount) {
        this.loginFailureCount = loginFailureCount;
    }

    @Column
    public Date getLockedDate() {
        return lockedDate;
    }

    public void setLockedDate(Date lockedDate) {
        this.lockedDate = lockedDate;
    }

    @Column
    public String getRegisterIp() {
        return registerIp;
    }

    public void setRegisterIp(String registerIp) {
        this.registerIp = registerIp;
    }

    @Column
    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    @Column
    public Date getLoginDate() {
        return loginDate;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column
    @JsonProperty
    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    @Column
    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Column
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Column
    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    @Column
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Column
    @JsonProperty
    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Column
    public String getCardNo() {
        return cardNo;
    }

    public void setCardNo(String cardNo) {
        this.cardNo = cardNo;
    }

    @Column
    @JsonProperty
    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    @Column
    @JsonProperty
    public String getLogo() {
        return logo;
    }

    public void setLogo(String logo) {
        this.logo = logo;
    }

    @Column
    @JsonProperty
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    @Column
    @JsonProperty
    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    @Column
    @JsonProperty
    public int getMemberType() {
        return memberType;
    }

    public void setMemberType(int memberType) {
        this.memberType = memberType;
    }

    @JsonProperty
    @NotNull(groups = Save.class)
    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(nullable = false, precision = 27, scale = 12)
    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }

    @JsonProperty
    @NotNull(groups = Save.class)
    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(nullable = false, precision = 27, scale = 12)
    public BigDecimal getCurrencyBalance() {
        return currencyBalance;
    }

    public void setCurrencyBalance(BigDecimal currencyBalance) {
        this.currencyBalance = currencyBalance;
    }


    @JsonProperty
    @NotNull(groups = Save.class)
    @Min(0)
    @Digits(integer = 12, fraction = 3)
    @Column(nullable = false, precision = 27, scale = 12)
    public BigDecimal getVirtualCurrency() {
        return virtualCurrency;
    }

    public void setVirtualCurrency(BigDecimal virtualCurrency) {
        this.virtualCurrency = virtualCurrency;
    }

    @Column
    @JsonProperty
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    @Column
    @JsonProperty
    public Boolean getIsMarketable() {
        return isMarketable;
    }

    public void setIsMarketable(Boolean isMarketable) {
        this.isMarketable = isMarketable;
    }

    @Column
    @JsonProperty
    public String getShopTitle() {
        return shopTitle;
    }

    public void setShopTitle(String shopTitle) {
        this.shopTitle = shopTitle;
    }

    @Column
    @JsonProperty
    public Boolean getIsTop() {
        return isTop;
    }

    public void setIsTop(Boolean isTop) {
        this.isTop = isTop;
    }

    @Column
    @JsonProperty
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column
    @JsonProperty
    public String getShopBanner() {
        return shopBanner;
    }

    public void setShopBanner(String shopBanner) {
        this.shopBanner = shopBanner;
    }

    /**
     * 获取购物车
     *
     * @return 购物车
     */
  /*  @OneToOne(mappedBy = "member", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE)
    public Cart getCart() {
        return cart;
    }
*/
    @Column
    @JsonProperty
    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

    /**
     * 设置购物车
     *
     * @param cart
     *            购物车
     */
   /* public void setCart(Cart cart) {
        this.cart = cart;
    }
*/
    @Column
    @JsonProperty
    public String getbLicense() {
        return bLicense;
    }

    public void setbLicense(String bLicense) {
        this.bLicense = bLicense;
    }

    @Column
    @JsonProperty
    public String getbLicenseNo() {
        return bLicenseNo;
    }

    public void setbLicenseNo(String bLicenseNo) {
        this.bLicenseNo = bLicenseNo;
    }

    @Column
    @JsonProperty
    public String getbBankName() {
        return bBankName;
    }

    public void setbBankName(String bBankName) {
        this.bBankName = bBankName;
    }

    @Column
    @JsonProperty
    public String getbBankCardNo() {
        return bBankCardNo;
    }

    public void setbBankCardNo(String bBankCardNo) {
        this.bBankCardNo = bBankCardNo;
    }

    @Column
    @JsonProperty
    public String getbName() {
        return bName;
    }

    public void setbName(String bName) {
        this.bName = bName;
    }

    @Column
    @JsonProperty
    public String getbCardNo() {
        return bCardNo;
    }

    public void setbCardNo(String bCardNo) {
        this.bCardNo = bCardNo;
    }

    @Column
    @JsonProperty
    public String getCpId() {
        return cpId;
    }

    public void setCpId(String cpId) {
        this.cpId = cpId;
    }

    @Column
    @JsonProperty
    public String getWxOpenId() {
        return wxOpenId;
    }

    public void setWxOpenId(String wxOpenId) {
        this.wxOpenId = wxOpenId;
    }

    @JsonProperty
    @Transient
    public String getMobileInfo(){
        try {
            return getMobile().substring(0, 3) + "****" + getMobile().substring(7, getMobile().length());
        }catch (Exception e){
            return "无法获取";
        }
    }
}

